//
//  LFBReadDetailViewController.m
//  ONE
//
//  Created by dllo on 16/6/10.
//  Copyright © 2016年 GroupNine. All rights reserved.
//

#import "LFBReadDetailViewController.h"
#import "LFBReadShortModel.h"
#import "LFBReadSerialModel.h"
#import "LFBReadQuestionModel.h"
#import "LFBReadDetailView.h"

@interface LFBReadDetailViewController () <GMCPagingScrollViewDelegate, GMCPagingScrollViewDataSource>
{
    AAPullToRefresh *pullToRefreshLeft;
    AAPullToRefresh *pullToRefreshRight;
}

@property (nonatomic, strong) GMCPagingScrollView *pagingScrollView;

@end

@implementation LFBReadDetailViewController
{
    // 文章类型
    LYLReadType readType;
}

- (void)dealloc
{
    _pagingScrollView.delegate = nil;
    _pagingScrollView.dataSource = nil;
    
    pullToRefreshLeft.showPullToRefresh = NO;
    pullToRefreshRight.showPullToRefresh = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    BaseModel *model = [_dataSource firstObject];
    if (model) {
        if ([model isKindOfClass:[LFBReadShortModel class]]) {
            self.title = @"短篇";
            readType = LYLReadTypeShort;
        }
        else if ([model isKindOfClass:[LFBReadSerialModel class]])
        {
            self.title = @"连载";
            readType = LYLReadTypeSerial;
        }
        else if ([model isKindOfClass:[LFBReadQuestionModel class]])
        {
            self.title = @"问题";
            readType = LYLReadTypeQuestion;
        }
        else
        {
            return;
        }
        
        [self initData];
        [self createPagingScrollView];
        [_pagingScrollView setCurrentPageIndex:0 reloadData:YES];
    }
}

#pragma mark --初始化数据
- (void)initData
{
    
}

#pragma mark --创建翻页滚动视图
- (void)createPagingScrollView
{
    __weak typeof(self) weakSelf = self;
    
    _pagingScrollView = [GMCPagingScrollView new];
    _pagingScrollView.backgroundColor = LYLViewControllerBGColor;
    [_pagingScrollView registerClass:[LFBReadDetailView class] forReuseIdentifier:kLFBReadDetailViewID];
    _pagingScrollView.delegate = self;
    _pagingScrollView.dataSource = self;
    _pagingScrollView.pageInsets = UIEdgeInsetsZero;
    _pagingScrollView.interpageSpacing = 0;
    
    pullToRefreshLeft = [_pagingScrollView.scrollView addPullToRefreshPosition:AAPullToRefreshPositionLeft actionHandler:^(AAPullToRefresh *v) {
        [weakSelf refresh];
        [v performSelector:@selector(stopIndicatorAnimation) withObject:nil afterDelay:1];
    }];
    // 临界值
    pullToRefreshLeft.threshold = 100;
    pullToRefreshLeft.borderColor = LYLAppThemeColor;
    pullToRefreshLeft.borderWidth = 4;
    pullToRefreshLeft.imageIcon = [UIImage new];
    
    pullToRefreshRight = [_pagingScrollView.scrollView addPullToRefreshPosition:AAPullToRefreshPositionRight actionHandler:^(AAPullToRefresh *v) {
        [weakSelf loadMore];
        [v performSelector:@selector(stopIndicatorAnimation) withObject:nil afterDelay:1];
    }];
    pullToRefreshRight.borderColor = LYLAppThemeColor;
    pullToRefreshRight.borderWidth = 4;
    pullToRefreshRight.imageIcon = [UIImage new];
    
    [self.view addSubview:_pagingScrollView];
    [_pagingScrollView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

#pragma mark --刷新视图
- (void)refresh
{
    [_pagingScrollView setCurrentPageIndex:0 reloadData:NO];
}

#pragma mark --加载更多视图
- (void)loadMore
{
    [_pagingScrollView setCurrentPageIndex:(_dataSource.count - 1) reloadData:NO];
}

#pragma mark --GMCPagingScrollViewDataSource
- (NSUInteger)numberOfPagesInPagingScrollView:(GMCPagingScrollView *)pagingScrollView
{
    return _dataSource.count;
}

- (UIView *)pagingScrollView:(GMCPagingScrollView *)pagingScrollView pageForIndex:(NSUInteger)index
{
    LFBReadDetailView *view = [pagingScrollView dequeueReusablePageWithIdentifier:kLFBReadDetailViewID];
    [view prepareForReuseWithViewType:readType];
    if (index == 0) {
        [view configureViewWithReadModel:_dataSource[index] type:readType atIndex:index inViewController:self];
    }

    return view;
}

#pragma mark --GMCPagingScrollViewDelegate
- (void)pagingScrollViewDidScroll:(GMCPagingScrollView *)pagingScrollView
{
    if (_pagingScrollView.isDragging) {
        CGPoint contentOffset = pagingScrollView.scrollView.contentOffset;
        pagingScrollView.scrollView.contentOffset = CGPointMake(contentOffset.x, 0);
    }
}

- (void)pagingScrollView:(GMCPagingScrollView *)pagingScrollView didScrollToPageAtIndex:(NSUInteger)index
{
    if (index != 0 && (!pagingScrollView.scrollView.isTracking || !pagingScrollView.scrollView.isDecelerating)) {
        LFBReadDetailView *view = [pagingScrollView pageAtIndex:index];
        [view configureViewWithReadModel:_dataSource[index] type:readType atIndex:index inViewController:self];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
